package com.xiaodu.acer.phoneplay;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.xiaodu.acer.phoneplay.common.util.PermissionUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import zlc.season.rxdownload3.RxDownload;
import zlc.season.rxdownload3.core.Status;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

/**
 * @author 小杜
 *         邮箱:m15701207383@163.com
 * @version V1.0
 * @Description: ${TODO}(用一句话描述该文件做什么)
 * @date 2018/1/22 11:13
 */

public class DownTestActivity extends Activity {
    private static final String api =
     "http://shouji.360tpcdn.com/170919/e7f5386759129f378731520a4c953213/com.eg.android.AlipayGphone_115.apk";
    private Button mStartButton;
    private Button mPauseButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mStartButton = findViewById(R.id.start);
        mPauseButton = findViewById(R.id.pause);
        RxDownload.INSTANCE.create(api)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Status>() {
                    @Override
                    public void accept(Status status) throws Exception {
                        System.out.println(status.getDownloadSize());
                    }
                });

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtil.requestPermisson
                        (mStartButton.getContext(), WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    download();
                                }
                            }
                        });
            }
        });

        mPauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pause();
            }
        });

    }

    private void download() {
        RxDownload.INSTANCE.start(api).subscribe();
    }


    private void pause() {
        RxDownload.INSTANCE.stop(api).subscribe();
    }
}
